//
//  CalendarMonth+Planner.m
//  planner
//
//  Created by 함동균 on 2014. 3. 21..
//  Copyright (c) 2014년 idea. All rights reserved.
//

#import "CalendarMonth+Planner.h"
#import "CalendarDay+Create.h"

@implementation CalendarMonth (Planner)

+ (CalendarMonth *)setYear:(int16_t)year month:(int16_t)month inManagedObjectContext:(NSManagedObjectContext *)context
{
    CalendarMonth *myCalendarMonth = nil;
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"CalendarMonth"];
    NSMutableArray *predicateArray = [NSMutableArray array];
    [predicateArray addObject:[NSPredicate predicateWithFormat:@"year = %d", year]];
    [predicateArray addObject:[NSPredicate predicateWithFormat:@"month = %d", month]];
    request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray];
    
    NSError *error;
    NSArray *matches = [context executeFetchRequest:request error:&error];
    
    if (!matches || error || [matches count] > 1) {
        // Handle error
    } else if ([matches count]) {
        //[matches.firstObject NSLogCalendarMonth];  //debug Calendar Month Data
        return matches.firstObject;
    } else {
        myCalendarMonth = [NSEntityDescription insertNewObjectForEntityForName:@"CalendarMonth"
                                                        inManagedObjectContext:context];
        myCalendarMonth.year = year;
        myCalendarMonth.month = month;
        [myCalendarMonth updateDays];
    }
    
    return myCalendarMonth;
}

- (void)updateDays
{
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    NSDate *date = nil;
    
    
    if (self.year > 0 && self.month > 0)
    {
        // Get first date of year month
        components.year = self.year;
        components.month = self.month;
        components.day = 1;
        date = [calendar dateFromComponents:components];
        
        // Get firat date of year month grid
        NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitCalendar;
        components = [calendar components:unit fromDate:date];
        int16_t weekday = components.weekday;
        components = [[NSDateComponents alloc] init];
        components.day = 1 - weekday;
        date = [calendar dateByAddingComponents:components toDate:date options:0];
        components.day = 1;
        
        for (weekday = 0; weekday < 7; weekday++) {
            for (int16_t weekdayOrdial = 0; weekdayOrdial < 6; weekdayOrdial++) {
                CalendarDay *day = [CalendarDay setDate:date
                                 inManagedObjectContext:[self managedObjectContext]];
                day.week = weekdayOrdial;
                day.day = weekday;
                if ([day equalsToDate:[NSDate date]])
                    day.isToday = YES;
                else
                    day.isToday = NO;
                day.isSelected = NO;
                
                [day addYearmonthObject:self];
                //[self addDaysObject:day];
                date = [calendar dateByAddingComponents:components toDate:date options:0];
            }
        }
    }
}

- (void)NSLogCalendarMonth
{
    NSLog(@"Calendar Month Log - Year:%d, Month:%d, Range:%d", self.year, self.month, self.days.count);
    
    for (int week = 1; week <= 6; week++) {
           NSLog(@" - week %d : %@  %@  %@  %@  %@  %@  %@", week,
              [[self.days objectAtIndex:week*7-7] getDateString],
              [[self.days objectAtIndex:week*7-6] getDateString],
              [[self.days objectAtIndex:week*7-5] getDateString],
              [[self.days objectAtIndex:week*7-4] getDateString],
              [[self.days objectAtIndex:week*7-3] getDateString],
              [[self.days objectAtIndex:week*7-2] getDateString],
              [[self.days objectAtIndex:week*7-1] getDateString]);
    }
}
@end
